package com.drama.outtake.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.drama.outtake.MyException;
import com.drama.outtake.common.Result;
import com.drama.outtake.domain.Shop;
import com.drama.outtake.domain.SimpleShop;
import com.drama.outtake.dto.ShopDetailDto;
import com.drama.outtake.service.IShopService;
import com.drama.outtake.service.IStarService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author : 李先均
 * @Date 2022/12/22 22:52
 */
@RestController
@RequestMapping("/shop")
@RequiredArgsConstructor
@Slf4j
public class ShopController {
    private final IShopService shopService;
    private final IStarService starService;

    @GetMapping("/{shopId}")
    public Result<ShopDetailDto> getShopDetailById(@PathVariable("shopId") Long shopId) {
        return Result.ok(shopService.getShopDetailById(shopId));
    }

    @GetMapping("/comment/{shopId}")
    public Result<Shop> getShopById(@PathVariable("shopId") Long shopId) {
        Shop shop = shopService.getShopById(shopId);
        return Result.ok(Shop.builder()
                .id(shop.getId())
                .name(shop.getName())
                .picture(shop.getPicture())
                .build());
    }

    @GetMapping("/updateStar")
    public Result<Object> updateStar(@RequestParam("shopId") Long shopId, @RequestParam("star") Boolean star) {
        if (!StpUtil.isLogin()) {
            throw new MyException("未登录，不能进行收藏操作");
        }
        starService.updateStar(shopId, star);
        return Result.ok();
    }

    @GetMapping("/star")
    public Result<List<Shop>> getStarShop() {
        List<Shop> shops = shopService.getStarShop();
        return Result.ok(shops);
    }

    //TODO 商家信息
    @GetMapping("/detail/{shopId}")
    public Result<Object> getShopDetail(@PathVariable("shopId") Long shopId) {
        Shop shop = shopService.getShopById(shopId);
        SimpleShop simpleShop = new SimpleShop();
        simpleShop.setAddress(shop.getAddress());
        simpleShop.setPhone(shop.getPhone());
        simpleShop.setSales(shop.getSales());
        simpleShop.setNotice(shop.getNotice());
        return Result.ok(simpleShop);
    }
}
